package com.auto.api.core.type.json.yapi;

import com.auto.api.common.utils.JsonMapper;
import com.auto.api.core.type.TypeFactory;
import com.auto.api.core.type.defaultvalue.ValueTypeHandler;
import com.auto.api.core.util.BeanUtil;
import java.util.Map;
import org.springframework.core.ResolvableType;

/**
 * @program:
 * @description: CommonJsonTypeHandler类型处理器
 * @author: liangshf
 * @create: 2020-09-24
 */
public class CommonJsonTypeHandler extends AbstractResultJsonTypeHandler {

    @Override
    public boolean canCovert(ResolvableType resolvableType) {
        return true;
    }

    @Override
    public String getResultJson(ResolvableType resolvableType, Map resultMap){
        ValueTypeHandler typeHandler = TypeFactory.getTypeHandler(resolvableType.resolve().getSimpleName());
        if (typeHandler != null) {
            resultMap.put(resolvableType.resolve().getSimpleName(), typeHandler.getTypeDefaultValue());
        } else {
            resultMap.putAll(BeanUtil.getClassColumnMap(resolvableType));
        }
        return JsonMapper.toJson(resultMap);
    }
}